/*
 * Project: babylon File Created at 2011-7-25 Copyright 2011 Alibaba.com Corporation Limited. All rights reserved. This
 * software is the confidential and proprietary information of Alibaba Company. ("Confidential Information"). You shall
 * not disclose such Confidential Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with Alibaba.com.
 */

package com.whale.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.whale.constants.ErrorCode;
import com.whale.entity.Response;
import com.whale.entity.User;

public class JSONUtil {

    public static Response<User> convertStringForLogin(String jsonString) {
        Response<User> response = new Response<User>();
        try {
            if (!StringUtil.isBlank(jsonString)) {
                JSONObject obj = new JSONObject(jsonString);
                boolean hasError = obj.getBoolean("hasError");
                if (hasError) {
                    String msg = obj.getString("error");
                    response.errorMsg = msg;
                    response.hasError = true;
                } else {
                    response.hasError = false;
                    User user = new User();
                    user.uid = obj.getString("UID");
                    response.code = ErrorCode.SUCCED.value();
                }
            } else {
                response.errorMsg = "请求出错";
            }
        } catch (JSONException e) {
            response.errorMsg = "解析出错";
            e.printStackTrace();
        }
        return response;
    }

    
    public static JSONArray convertString2ArrayJson(String jsonString) {
        JSONObject obj;
        try {
            obj = new JSONObject(jsonString);
            boolean hasError = obj.getBoolean("hasError");
            if (hasError) {
                return null;
            } else {
                jsonString = obj.getString("content");
                JSONArray jsonArray = new JSONArray(jsonString);
                return jsonArray;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONArray convertString2ArrayJson_Comments(String jsonString) {
        JSONObject obj;
        try {
            obj = new JSONObject(jsonString);
            boolean hasError = obj.getBoolean("hasError");
            if (hasError) {
                return null;
            } else {
                JSONObject jsoContent = obj.getJSONObject("content");
                jsonString = jsoContent.getString("comments");
                JSONArray jsonArray = new JSONArray(jsonString);
                return jsonArray;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 直接是Array的json字符串解析
     * 
     * @param jsonString
     * @return JSONArray，若解析出错等返回null
     */
    public static JSONArray getJSONArray(String jsonString) {
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            return jsonArray;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 能解析返回JSONObject，若格式解析不了返回null
     * 
     * @param jsonString
     * @return
     */
    public static JSONObject getJSONObject(String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject convertString2JsonObject(String jsonString) {
        JSONObject obj;
        try {
            obj = new JSONObject(jsonString);
            boolean hasError = obj.getBoolean("hasError");
            if (hasError) {
                return null;
            } else {
                jsonString = obj.getString("content");
                JSONObject jso = new JSONObject(jsonString);
                return jso;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
